package com.wu.flandre.util;

import com.wu.flandre.component.GlobalConstant;
import com.wu.flandre.entity.wechat.CreateXmlMsgEntity;
import com.wu.flandre.entity.wechat.WeChatMsgTextEntity;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * XMl工具类
 *
 * @author 50816
 * @date 2019年12月1日 19:35:43
 */
@Component
@Slf4j
public class XmlUtil {
    private SAXReader reader = new SAXReader();

    /**
     * 通用方法XMl转Map
     *
     * @param request
     * @return
     */
    public Map<String, Object> commonXmlToMap(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            InputStream inputStream = request.getInputStream();
            Document doc = reader.read(inputStream);

            Element rootElement = doc.getRootElement();
            Iterator iterator = rootElement.elementIterator();
            while (iterator.hasNext()) {
                Element el = (Element) iterator.next();
                result.put(el.getName(), el.getText());
            }
        } catch (Exception e) {
            log.error("commonXmlToMap异常:" + e.getMessage());
        }
        return result;
    }

    /**
     * XMl的文本消息实体类
     *
     * @param request
     * @return
     */
    public WeChatMsgTextEntity XmlToTextEntity(HttpServletRequest request) {
        WeChatMsgTextEntity textEntity = new WeChatMsgTextEntity();
        try {
            InputStream inputStream = request.getInputStream();
            Document doc = reader.read(inputStream);

            Element root = doc.getRootElement();
            textEntity.setToUserName(root.elementText("ToUserName"));
            textEntity.setFromUserName(root.elementText("FromUserName"));
            textEntity.setCreateTime(root.elementText("CreateTime"));
            textEntity.setMsgType(root.elementText("MsgType"));
            textEntity.setContent(root.elementText("Content"));
            textEntity.setMsgId(root.elementText("MsgId"));
        } catch (Exception e) {
            log.error("XmlToTextEntity异常:" + e.getMessage());
        }
        return textEntity;
    }

    /**
     * 创建XMl消息的实体类
     *
     * @param msgEntity
     * @param msgType
     * @return
     */
    public String createXml(CreateXmlMsgEntity msgEntity, String msgType) {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("xml");
        root.addElement("ToUserName").addCDATA(msgEntity.getToUserName());
        root.addElement("FromUserName").addCDATA(msgEntity.getFromUserName());
        root.addElement("CreateTime").addText(msgEntity.getCreateTime().toString());
        if (GlobalConstant.TEXT_MSG_TYPE.equals(msgType)) {
            root.addElement("MsgType").addCDATA(msgEntity.getMsgType());
            root.addElement("Content").addCDATA(msgEntity.getContent());
        } else {
            log.error("没有找到对应的消息类型,创建XML失败");
            return null;
        }
        return document.asXML();
    }
}
